/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketbooking;

import DAO.DAO_ChuyenTau;
import DAO.DAO_Ga;
import DBTransmit.TableGaRecord;
import MessageVariable.ClientSign;
import MessageVariable.ResponseFromServer;
import MessageVariable.ServerSign;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

/**
 *
 * @author LEHOANGKHANH
 */
public class RemoteController implements Runnable{
    ServerSign serverSign = null;
    ServerKey serverKey = null;
    ClientSign clientSign = null;
    SecretKey sessionKey = null;
    Socket sessionSocket = null;
    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    
    public RemoteController(ServerSign serversign, ClientSign clientsign, ServerKey key, Socket sock){
        try {
            this.serverSign = serversign;
            this.clientSign = clientsign;
            this.sessionSocket = sock;
            this.serverKey = key;
            out = new ObjectOutputStream(sessionSocket.getOutputStream());
            in = new ObjectInputStream(sessionSocket.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(RemoteController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @Override
    public void run(){
        try {
            Object o = null;
            while( (o = in.readObject()) != null){
                if(o instanceof SealedObject){
                    
                }
              
            }
        } catch (IOException ex) {
            Logger.getLogger(RemoteController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RemoteController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void processQueryGaList(TransferMessage msg){
        TransferMessage response = new TransferMessage();
        response.setType(TransferMessage.DATA);
        response.setServerSign(serverSign);
        List<TableGaRecord> lst = DAO_Ga.LoadDS(sessionKey);
        response.setStationList(lst);
        //ma hoa
        //gui di
    }
    private void processQueryDay(TransferMessage msg){
        TransferMessage response = new TransferMessage();
        response.setType(TransferMessage.DATA);
        response.setServerSign(serverSign);
        List<ResponseFromServer> lst = DAO_ChuyenTau.LoadDSByDay(msg.getRequestForServer().getNgayKhoiHanh(), serverKey.getSecretKey());
        response.setResponse(lst);
        //ma hoa
        //gui di
    }
    private void processQueryStation(TransferMessage msg){
        TransferMessage response = new TransferMessage();
        response.setType(TransferMessage.DATA);
        response.setServerSign(serverSign);
        List<ResponseFromServer> lst = DAO_ChuyenTau.LoadDSByGa(msg.getRequestForServer().getGaDi(), msg.getRequestForServer().getGaDen(), serverKey.getSecretKey());
        response.setResponse(lst);
        //ma hoa
        //gui di
    }
    private void processQueryDayAndStation(TransferMessage msg){
        TransferMessage response = new TransferMessage();
        response.setType(TransferMessage.DATA);
        response.setServerSign(serverSign);
        List<ResponseFromServer> lst = DAO_ChuyenTau.LoadDSByDay_Ga(msg.getRequestForServer().getNgayKhoiHanh(), msg.getRequestForServer().getGaDi(), msg.getRequestForServer().getGaDen(), serverKey.getSecretKey());
        response.setResponse(lst);
        //ma hoa
        //gui di
    }
    private void processDatVe(TransferMessage msg){
       
    } 
    private void processHuyVe(TransferMessage msg){
        //code
    }
    private void processTTThongTin(TransferMessage msg){
        //code
    }
    private void processTroGiup(TransferMessage msg){
        //code
    }
    private void processVeDaDat(TransferMessage msg){
        //code
    }
}
